// driver.js
// 获取应用实例
const app = getApp()

Page({
	data: {
		dataList:[],
		page:1,
		next:false,
		loading:true,
		htmlLoading:false,
		
		scrolltop:0,
		dotype:{
			data:[
				{text:'不限',value:0},
				{text:'只干长期',value:2},
				{text:'只干替班',value:1},
			],
			val:0
		},
		shebei:{
			popup:false,
			param:{
				activeId: 0,
				activeText:'',
			},
			component:false
		},
		InitCity:{
			popup:false,
			component:false,
			province:'',
			city:'',
			isprovince:1,
			iscity:1
		},
	},
	dotypechange({ detail }){
		this.setData({
			"dotype.val":detail,
		});
		this.GetData();
	},
	cityPopupOpen(){
		this.setData({
			"InitCity.popup":true,
			"InitCity.component":true
		});
	},
	ChangeCityVal(val){
		var province = val.detail.province;
		var city = val.detail.city;
		this.cityPopupClose();
		if(province!=this.data.InitCity.province || city!=this.data.InitCity.city){
			this.setData({
				"InitCity.province":province,
				"InitCity.city":city
			});
			this.GetData();
		}
		
	},
	cityPopupClose(){
		this.selectComponent('#DropdownCityPopup').toggle(false);
		this.setData({
			"InitCity.popup":false
		});
	},
	cityPopupAfterLeave(){
		this.setData({
			"InitCity.component":false
		});
	},
	
	ShebeiPopupOpen(){
		this.setData({
			"shebei.popup":true,
			"shebei.component":true
		});
		
	},
	ShebeiChangeVal(val){
		var sid = val.detail.sid;
		var text = val.detail.text;
		this.ShebeiPopupClose();
		if(parseInt(this.data.shebei.param.activeId) != parseInt(sid)){
			this.setData({
				"shebei.param.activeId":sid,
				"shebei.param.activeText":text
			});
			this.GetData();
		}
	},
	ShebeiPopupClose(){
		this.selectComponent('#DropdownShebeiPopup').toggle(false);
		this.setData({
			"shebei.popup":false
		});
	},
	ShebeiPopupAfterLeave(){
		this.setData({
			"shebei.component":false
		});
	},

	handleDetail(event){
		if(!app.globalData.hasUserInfo){
			app.handleLogin();
			return false;
		};
		var id = event.currentTarget.dataset.id;
		if(parseInt(id)){
			wx.navigateTo({
				url: '../driverdetail/driverdetail?id='+id,
			});
		}else{
			wx.showToast({
				title: '数据错误，请稍后再试',
				icon: 'error',
				duration: 2000
			});
		}
		
	},

	GetData(type){
		var self = this;
		var param = {};
		var pdata = this.data;
		if(type=='scroll' && parseInt(pdata.page)>1){
			param['page'] = parseInt(pdata.page);
		}else{
			wx.showLoading({
				title: '加载中...',
				mask:true
			});
			this.setData({
				page:1,
				scrolltop:0,
				loading:true,
			});
		}
		if(parseInt(pdata.shebei.param.activeId)>0){
			param['sid'] = parseInt(pdata.shebei.param.activeId);
		}
		if(pdata.InitCity.province){
			param['province'] = pdata.InitCity.province;
		}
		if(parseInt(pdata.dotype.val)>-1){
			param['dotype'] = pdata.dotype.val;
		}
		
		wx.request({
			url: 'https://www.grabinter.com/Personnotes/index',
			method: 'POST',
			header: {
				"Content-Type": 'application/x-www-form-urlencoded'
			},
			data:param,
			success: json => {
				var data = json.data;
				var arr = [];
				for(var i in data.data){
					var item = data.data[i];
					if(parseInt(item.sid)>0){
						for(var i in app.globalData.shebei){
							for(var x in app.globalData.shebei[i].children){
								const itgem = app.globalData.shebei[i].children[x];
								if(itgem.sid == item.sid){
									item['sid'] = itgem.text
								}
							}
						}
					}
					arr.push(item);
				}
				if(type=='scroll'){
					var cont = this.data.dataList.concat(arr);
				}else{
					var cont = arr;
					wx.hideLoading();
				}
				self.setData({
					dataList:cont,
					next:data.next,
					page:data.next?data.page:self.data.page,
					htmlLoading:false,
					loading:false,
				});
				
	
			}
		});
	},
	LoadMore(){
		if(!this.data.next){
			return false;
		}
		if(this.data.htmlLoading){
			return false
		}
		this.setData({
			htmlLoading:true,
		});
		this.GetData('scroll')
	},
	onLoad() {
		var val = app.globalData.DefaultCity;
		if(val != 'not'){
			val = val.split(',');
			this.setData({
				"InitCity.province":val[0],
			});
		}
		this.GetData();
	},
	onShow: function() {
		
	},

})
